package com.example.classcommunity3.service;

import com.alibaba.fastjson.JSON;
import com.example.classcommunity3.dao.ClasstableDao;
import com.example.classcommunity3.model.Classtable;
import com.example.classcommunity3.model.Subject;
import com.example.classcommunity3.model.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ClasstableService {

    @Autowired
    ClasstableDao classtabledao;

    @Autowired
    SubjectService subjectService;

    @Autowired
    TeacherService teacherService;

    public boolean getSubject(String subCode, Long account) {
        /**
         @describe 查询并判断是否已加入的课程
         @param [subCode, account]
         @return boolean
         @date 2018/6/10
         */
        Classtable classtable = classtabledao.findByCSubCodeAndCStuAccount(subCode, account);
        if (classtable == null)
            return false;
        else
            return true;
    }


    public boolean joinSub(String subCode, Long account) {
        /**
         @describe 加入课程
         @param [subCode 课程邀请码, account 用户名]
         @return boolean
         @date 2018/6/10
         */
        //判断该课程是否存在
        if (subjectService.existClass(subCode)){

            //判断学生是否已经加入该课程
            if (!getSubject(subCode, account)) {
                Classtable insertClass = new Classtable();

                insertClass.setcStuAccount(account);
                insertClass.setcSubCode(subCode);
                classtabledao.save(insertClass);

                subjectService.updateStuNumber(subCode);

                return true;
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }
    }

//    public boolean deleteSub(String subCode, Long account) {
//        /**
//         @describe 删除课程
//         @param [subCode, account]
//         @return boolean
//         @date 2018/6/6
//         */
//        Classtable classtable = classtabledao.findByCSubCodeAndCStuAccount(subCode, account);
//        classtabledao.delete(classtable);
//        subjectService.updateStuNumber(subCode);
//        return true;
//
//    }

    public boolean deleteAllSub(String subCode){
        try {
            classtabledao.deleteByCSubCode(subCode);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public Long getstunumber(String subCode) {
        /**
         @describe 获得学生人数
         @param [subCode]
         @return java.lang.Long
         @date 2018/6/10
         */
        return classtabledao.countByCSubCode(subCode);
    }

    public String getAllStuSub(Long account) {
        /**
         @describe 得到当前学生选的课
         @param [account]
         @return java.lang.String 返回String json
         @date 2018/6/10
         */
        List<Classtable> list = classtabledao.findByCStuAccount(account);
        List<HashMap> returnList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            Classtable classtable = list.get(i);
            String subCode = classtable.getcSubCode();
            Subject subject = subjectService.getSubInfo(subCode);
            String subName = subject.getSubName();
            Long subStuNumber = subject.getSubStuNumber();
            Long subTeacAccount = subject.getSubTAccount();
            String tName = teacherService.getTName(subTeacAccount);

            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("subCode", subCode);
            hashMap.put("subName", subName);
            hashMap.put("subStuNumber", subStuNumber);
            hashMap.put("tName", tName);

            returnList.add(hashMap);
        }

        String returnJson = JSON.toJSONString(returnList);
        return returnJson;
    }
}
